package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Transactional
public class DishServiceImpl implements DishService {
    @Autowired(required = false)
    private DishMapper dishMapper;
    @Autowired(required = false)
    private DishFlavorMapper dishFlavorMapper;
    @Autowired(required = false)
    private CategoryMapper categoryMapper;
    @Autowired(required = false)
    private RedisTemplate redisTemplate;

    @Override
    @Transactional
    public Boolean save(DishDto dishDto) {
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setSort(0);
        dishDto.setStatus(1);
        dishMapper.save(dishDto);

        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setCreateTime(LocalDateTime.now());
            flavor.setUpdateTime(LocalDateTime.now());
            flavor.setDishId(dishDto.getId());
            flavor.setCreateUser(dishDto.getCreateUser());
            flavor.setUpdateUser(dishDto.getUpdateUser());
        }

        dishFlavorMapper.add(flavors);
        redisTemplate.delete("dish_"+dishDto.getCategoryId()+"_1");
        return true;
    }

    //连表查询
    @Override
    public Page<DishDto> findByPage(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page, pageSize);
        List<DishDto> dishDtoList = dishMapper.findAll(name);
        PageInfo<DishDto> pageInfo = new PageInfo<>(dishDtoList);

        Page<DishDto> pageObj = new Page<>();
        pageObj.setTotal(pageInfo.getTotal());
        pageObj.setRecords(pageInfo.getList());
        return pageObj;
    }


    //分表查询
    @Override
    public Page<DishDto> findByPage2(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page, pageSize);
        List<Dish> dishList = dishMapper.findAll2(name);
        PageInfo<Dish> pageInfo = new PageInfo<>(dishList);
        List<DishDto> dishDtoList = new ArrayList<>();
        //对每一个dish菜品对象进行循环
        for (Dish dish : dishList) {
            DishDto dishDto = new DishDto();
            //dish里面所有的数据赋值给dishDto对象里面
            BeanUtils.copyProperties(dish, dishDto);
            Category category = categoryMapper.findById(dishDto.getCategoryId());
            //将分类对象中分类名称赋值给dishDto对象中
            dishDto.setCategoryName(category.getName());

            dishDtoList.add(dishDto);
        }

        Page<DishDto> pageObj = new Page<>();
        pageObj.setRecords(dishDtoList);
        pageObj.setTotal(pageInfo.getTotal());
        return pageObj;
    }

    @Override
    public DishDto findById(Long id) {
        DishDto dishDto = dishMapper.findById(id);
        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavors(DishDto dishDto) {
        dishDto.setUpdateTime(LocalDateTime.now());
        dishMapper.updateById(dishDto);

        dishFlavorMapper.deleteById(dishDto.getId());

        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
            flavor.setCreateTime(LocalDateTime.now());
            flavor.setUpdateTime(LocalDateTime.now());
            flavor.setCreateUser(dishDto.getCreateUser());
            flavor.setUpdateUser(dishDto.getUpdateUser());
        }
        dishFlavorMapper.add(flavors);
        Set keys = redisTemplate.keys("dish_*"); //获取所有以dish_xxx开头的key
        redisTemplate.delete(keys); //删除这些key
    }

    @Override
    public boolean deleteById(Long id) {
        DishDto dishDto = dishMapper.findById(id);
        redisTemplate.delete("dish_"+dishDto.getCategoryId()+"1");
        return dishMapper.deleteById(id) > 0;
    }

    @Override
    public boolean updateStatus(Long[] ids, Integer status) {
        for (Long id : ids) {
            DishDto dishDto = dishMapper.findById(id);
            redisTemplate.delete("dish_"+dishDto.getCategoryId()+"_1");
        }
        return dishMapper.updateStatusById(ids, status) > 0;
    }

//    @Override
//    public boolean updateStatus2(Long[] ids) {
//        return dishMapper.updateStatusById2(ids)>0;
//    }

    @Override
    public void deleteByIds(Long[] ids) {
        for (Long id : ids) {
            DishDto dishDto = dishMapper.findById(id);
            redisTemplate.delete("dish_"+dishDto.getCategoryId()+"_1");
        }
        dishMapper.deleteByIds(ids);
    }

//    @Override
//    public List<Dish> findByCategoryId(Long categoryId) {
//        List<Dish> dishList = dishMapper.findByCategoryId(categoryId);
//        return dishList;
//    }

    @Override
    public Dish findByName(String name) {
        Dish dish = dishMapper.findByName(name);
        return dish;
    }

    @Override
    public List<DishDto> findByCategoryIdAndStatus(Long categoryId, Integer status, String name) {
        String key = "dish_" + categoryId + "_" + status;
        List<DishDto> dishDtoList = (List<DishDto>) redisTemplate.opsForValue().get(key);
        if (dishDtoList == null) {
            List<Dish> dishList = dishMapper.findByCategoryIdAndStatus(categoryId, status, name);
            dishDtoList = dishList.stream().map((dish -> {
                DishDto dishDto = new DishDto();
                BeanUtils.copyProperties(dish, dishDto);

                List<DishFlavor> flavors = dishFlavorMapper.findFlavorsById(dishDto.getId());
                dishDto.setFlavors(flavors);
                return dishDto;
            })).collect(Collectors.toList());
            redisTemplate.opsForValue().set(key,dishDtoList);
        }
        return dishDtoList;
    }
}